home *** CD-ROM | disk | FTP | other *** search
/ NOVA - For the NeXT Workstation / NOVA - For the NeXT Workstation.iso / Documents / NeXTAnswers / objc.504 < prev    next >
Text File  |  1992-02-06  |  1KB  |  55 lines

  1. Storage
  2.  
  3. Q:  How do you store and access elements in the Storage class?
  4.  
  5. A:  Although Storage's instance variables permit you to access the data directly, this approach is discouraged.   Instead, use the Storage object's methods (such as addElement: and elementAt:) to store and access the elements.  
  6.  
  7. Following is a code snippet that creates an empty Storage object, adds new elements to it, and then references them.  The type-specific code is #defined for clarity.  Note that the elements need to be added and referenced as pointers.
  8.  
  9. #import <objc/Storage.h>
  10.  
  11. @interface SomeObject:Object
  12. @end
  13. @implementation SomeObject
  14.  
  15. #if 1
  16. #define TYPE char *
  17. #define PRINT(var1, var2)  printf("first = %s second = %s\n", var1, var2);
  18. #define VAL1 ("hello")
  19. #define VAL2 ("world")
  20. #else
  21. #define TYPE int
  22. #define PRINT(var1, var2)  printf("first = %d second = %d\n", var1, var2);
  23. #define VAL1 (-5)
  24. #define VAL2 (32)
  25. #endif
  26.  
  27. - appDidInit:sender
  28. {
  29.     Storage *store;
  30.     TYPE a = VAL1;
  31.     TYPE b = VAL2;
  32.     TYPE *a1;
  33.     TYPE *b1;
  34.     
  35.     store = [[Storage alloc] initCount:0 elementSize:sizeof(TYPE)
  36.                 description:@encode(TYPE)];
  37.     [store addElement:(void *)&a];
  38.     [store addElement:(void *)&b];
  39.     PRINT(a, b);
  40.  
  41.     a1 = (TYPE *) [store elementAt:0];
  42.     b1 = (TYPE *) [store elementAt:1];
  43.     PRINT(*a1, *b1);
  44.     
  45.     return self;
  46. }
  47.  
  48.  
  49.  
  50. QA504    
  51.  
  52. Valid for 2.0
  53.  
  54.  
  55.